<?php
namespace app\model;


class w_init {
    
static function getteamlist($system) { 
    global $_P, $_G;
    $themeid= intval($_P['themeid']);
    $company = intval($_P['company']);
    if ($system['ruanjian']==3){
        $shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$company}'");
        $shopname=$shop['name'];
    }
    $teamlist = db_select ( "SELECT * FROM " . tablename (t_team) .
        " WHERE  company = '{$company}' order by groupid" );
    $myworker = Worker::getThemeWorkerByOpenidzhuce($fans['openid'],$themeid,1);
    if (!empty($teamlist )){
        $team=db_find("SELECT * FROM ".tablename(t_team)." WHERE id= '{$myworker['team']}'");
        $teamname=$team['name'];
    }

    $result=array();
    $result['error']=0;
    $result['msg']='';
    $result['data']['shopname']=$shopname;
    $result['data']['teamname']=$teamname;
    $result['data']['teamlist']=$teamlist;
    $result['data']['myworker']=$myworker;
    return $result;
}
static function pass($system,$config) { 
    global $_P, $_G;
    $pass= intval($_P['pass']);
    $company = intval($_P['company']);
    if (empty($_G['member']['uid'])) {
        $result=array();
        $result['error']=1;
        $result['msg']='用户参数信息错误';
        $result['data']='';
        return $result;
    }
    if(empty($pass)){
        $result=array();
        $result['error']=1;
        $result['msg']='密码为空';
        $result['data']='';
        return $result;
    }
    if ($system['ruanjian']==3){
        if(empty($company)){
            $result=array();
            $result['error']=1;
            $result['msg']='请选择所属企业';
            $result['data']='';
            return $result;
        }
        $shop=db_find("SELECT * FROM ".tablename(t_shangjia)." WHERE id= '{$company}'");
        $shopname=$shop['name'];
        $shoppass=$shop['shoppass'];
        $workerpass=$shop['workerpass'];
    }else{
        $workerpass=$config['workerpass'];
    }
    if ($workerpass == $pass){
        $level = 'worker';
        $op='last';
        $leveln = '3';
    }elseif ($shoppass == $pass){
        $level = 'shop';
        $leveln = '2';
    }elseif ($system['adminpass'] == $pass){
        $level = 'admin';
        $leveln = '1';
    }else{
        $result=array();
        $result['error']=1;
        $result['msg']='密码错误';
        $result['data']='';
        return $result;
    }

    if ($system['ruanjian']==3){
        if ($level == 'worker' || $level == 'shop'){
            if ($myworker['company']!=$company && $myworker['company']!=0){
                $result=array();
                $result['error']=1;
                $result['msg']='您不可变更企业,如需变更请联系管理员,进行人事变动';
                $result['data']='';
                return $result;
            }
        }
    }

    $data = array (
        'uniacid' => $_G['mpid'],
        'uid'     => $_G['member']['uid'],
        'openid'  => $_G['openid'],
        'company' => $company,
        'status'  => 1,
        'level'  => $leveln,
        'type'    => 'theme',
    );


    $alltheme = Theme::getAllTheme();
    if (!empty($alltheme)) {
        foreach ($alltheme as $key => $item) {
            $data['themeid'] = $item['id'];
            $workersave = Worker::getThemeWorkerByOpenidzhuce($_G['openid'],$item['id'],1);
            if (empty($workersave)){
                $data ['addtime'] = TIMESTAMP;
                db_insert ( t_worker, $data );
            }else{
                db_update (t_worker, $data, array ('openid' => $_G['openid'],'themeid' => $item['id']));
            }
        }
    }

    if ($level == 'admin' || $level == 'shop'){
        $teamlist = db_select ( "SELECT * FROM " . tablename (t_team) .
            " WHERE  company = '{$company}' order by groupid" );
        $tuanshu = 5;
        foreach ($teamlist as $index =>$row){
            if ($row['groupid']==1){
                $tuan['1'] = $row['name'];
            }elseif ($row['groupid']==2){
                $tuan['2'] = $row['name'];
            }elseif ($row['groupid']==3){
                $tuan['3'] = $row['name'];
            }elseif ($row['groupid']==4){
                $tuan['4'] = $row['name'];
            }elseif ($row['groupid']==5){
                $tuan['5'] = $row['name'];
            }elseif ($row['groupid']==6){
                $tuan['6'] = $row['name'];
                if ($tuanshu < 6) {
                    $tuanshu = 6;
                }
            }elseif ($row['groupid']==7){
                $tuan['7'] = $row['name'];
                if ($tuanshu < 7) {
                    $tuanshu = 7;
                }
            }elseif ($row['groupid']==8){
                $tuan['8'] = $row['name'];
                if ($tuanshu < 8) {
                    $tuanshu = 8;
                }
            }elseif ($row['groupid']==9){
                $tuan['9'] = $row['name'];
                if ($tuanshu < 9) {
                    $tuanshu = 9;
                }
            }elseif ($row['groupid']==10){
                $tuan['10'] = $row['name'];
                if ($tuanshu < 10) {
                    $tuanshu = 10;
                }
            }elseif ($row['groupid']==11){
                $tuan['11'] = $row['name'];
                if ($tuanshu < 11) {
                    $tuanshu = 11;
                }
            }elseif ($row['groupid']==12){
                $tuan['12'] = $row['name'];
                if ($tuanshu < 12) {
                    $tuanshu = 12;
                }
            }elseif ($row['groupid']==13){
                $tuan['13'] = $row['name'];
                if ($tuanshu < 13) {
                    $tuanshu = 13;
                }
            }elseif ($row['groupid']==14){
                $tuan['14'] = $row['name'];
                if ($tuanshu < 14) {
                    $tuanshu = 14;
                }
            }elseif ($row['groupid']==15){
                $tuan['15'] = $row['name'];
                if ($tuanshu < 15) {
                    $tuanshu = 15;
                }
            }elseif ($row['groupid']==16){
                $tuan['16'] = $row['name'];
                if ($tuanshu < 16) {
                    $tuanshu = 16;
                }
            }elseif ($row['groupid']==17){
                $tuan['17'] = $row['name'];
                if ($tuanshu < 17) {
                    $tuanshu = 17;
                }
            }elseif ($row['groupid']==18){
                $tuan['18'] = $row['name'];
                if ($tuanshu < 18) {
                    $tuanshu = 18;
                }
            }elseif ($row['groupid']==19){
                $tuan['19'] = $row['name'];
                if ($tuanshu < 19) {
                    $tuanshu = 19;
                }
            }elseif ($row['groupid']==20){
                $tuan['20'] = $row['name'];
                if ($tuanshu < 20) {
                    $tuanshu = 20;
                }
            }elseif ($row['groupid']==21){
                $tuan['21'] = $row['name'];
                if ($tuanshu < 21) {
                    $tuanshu = 21;
                }
            }elseif ($row['groupid']==22){
                $tuan['22'] = $row['name'];
                if ($tuanshu < 22) {
                    $tuanshu = 22;
                }
            }elseif ($row['groupid']==23){
                $tuan['23'] = $row['name'];
                if ($tuanshu < 23) {
                    $tuanshu = 23;
                }
            }elseif ($row['groupid']==24){
                $tuan['24'] = $row['name'];
                if ($tuanshu < 24) {
                    $tuanshu = 24;
                }
            }elseif ($row['groupid']==25){
                $tuan['25'] = $row['name'];
                if ($tuanshu < 25) {
                    $tuanshu = 25;
                }
            }elseif ($row['groupid']==26){
                $tuan['26'] = $row['name'];
                if ($tuanshu < 26) {
                    $tuanshu = 26;
                }
            }elseif ($row['groupid']==27){
                $tuan['27'] = $row['name'];
                if ($tuanshu < 27) {
                    $tuanshu = 27;
                }
            }elseif ($row['groupid']==28){
                $tuan['28'] = $row['name'];
                if ($tuanshu < 28) {
                    $tuanshu = 28;
                }
            }elseif ($row['groupid']==29){
                $tuan['29'] = $row['name'];
                if ($tuanshu < 29) {
                    $tuanshu = 29;
                }
            }elseif ($row['groupid']==30){
                $tuan['30'] = $row['name'];
                if ($tuanshu < 30) {
                    $tuanshu = 30;
                }
            }
        }
    }
    $result=array();
    $result['error']=0;
    $result['msg']='';
    $result['data']['tuanshu']=$tuanshu;
    $result['data']['tuan']=$tuan;
    $result['data']['shopname']=$shopname;
    $result['data']['level']=$leveln;     
    return $result;

}
static function saveteam($system)
{
    global $_P, $_G;
    if (empty($_G['member']['uid'])) {
        $result=array();
        $result['error']=1;
        $result['msg']='用户参数信息错误';
        $result['data']='';
        return $result;
    }
    $themeid= intval($_P['themeid']);
    $myworker = Worker::getThemeWorkerByOpenidzhuce($_G['openid'],$themeid,1);
    $level = $myworker['level'];
    $company = intval($_P['company']);
    if ($level == '1' || $level == '2'){ 
        $teamlist = $_P['teamlist'];
        $teamlist= json_decode(htmlspecialchars_decode($teamlist)); 
        $tuan1 = $teamlist->tuan1;
        $tuan2 = $teamlist->tuan2;
        $tuan3 = $teamlist->tuan3;
        $tuan4 = $teamlist->tuan4;
        $tuan5 = $teamlist->tuan5;
        $tuan6 = $teamlist->tuan6;
        $tuan7 = $teamlist->tuan7;
        $tuan8 = $teamlist->tuan8;
        $tuan9 = $teamlist->tuan9;
        $tuan10 = $teamlist->tuan10;
        $tuan11 = $teamlist->tuan11;
        $tuan12 = $teamlist->tuan12;
        $tuan13 = $teamlist->tuan13;
        $tuan14 = $teamlist->tuan14;
        $tuan15 = $teamlist->tuan15;
        $tuan16 = $teamlist->tuan16;
        $tuan17 = $teamlist->tuan17;
        $tuan18 = $teamlist->tuan18;
        $tuan19 = $teamlist->tuan19;
        $tuan20 = $teamlist->tuan20;
        $tuan21 = $teamlist->tuan21;
        $tuan22 = $teamlist->tuan22;
        $tuan23 = $teamlist->tuan23;
        $tuan24 = $teamlist->tuan24;
        $tuan25 = $teamlist->tuan25;
        $tuan26 = $teamlist->tuan26;
        $tuan27 = $teamlist->tuan27;
        $tuan28 = $teamlist->tuan28;
        $tuan29 = $teamlist->tuan29;
        $tuan30 = $teamlist->tuan30;
        Worker::addTeam($company,$themeid,1,$tuan1);
        Worker::addTeam($company,$themeid,2,$tuan2);
        Worker::addTeam($company,$themeid,3,$tuan3);
        Worker::addTeam($company,$themeid,4,$tuan4);
        Worker::addTeam($company,$themeid,5,$tuan5);
        Worker::addTeam($company,$themeid,6,$tuan6);
        Worker::addTeam($company,$themeid,7,$tuan7);
        Worker::addTeam($company,$themeid,8,$tuan8);
        Worker::addTeam($company,$themeid,9,$tuan9);
        Worker::addTeam($company,$themeid,10,$tuan10);
        Worker::addTeam($company,$themeid,11,$tuan11);
        Worker::addTeam($company,$themeid,12,$tuan12);
        Worker::addTeam($company,$themeid,13,$tuan13);
        Worker::addTeam($company,$themeid,14,$tuan14);
        Worker::addTeam($company,$themeid,15,$tuan15);
        Worker::addTeam($company,$themeid,16,$tuan16);
        Worker::addTeam($company,$themeid,17,$tuan17);
        Worker::addTeam($company,$themeid,18,$tuan18);
        Worker::addTeam($company,$themeid,19,$tuan19);
        Worker::addTeam($company,$themeid,20,$tuan20);
        Worker::addTeam($company,$themeid,21,$tuan21);
        Worker::addTeam($company,$themeid,22,$tuan22);
        Worker::addTeam($company,$themeid,23,$tuan23);
        Worker::addTeam($company,$themeid,24,$tuan24);
        Worker::addTeam($company,$themeid,25,$tuan25);
        Worker::addTeam($company,$themeid,26,$tuan26);
        Worker::addTeam($company,$themeid,27,$tuan27);
        Worker::addTeam($company,$themeid,28,$tuan28);
        Worker::addTeam($company,$themeid,29,$tuan29);
        Worker::addTeam($company,$themeid,30,$tuan30);
    }
}

static function saveinfo($fans)
{

    global $_P, $_G;
    if (empty($_G['member']['uid'])) {
        $result=array();
        $result['error']=1;
        $result['msg']='用户参数信息错误';
        $result['data']='';
        return $result;
    }
    $team = intval($_P['team']);
    $teamlist = db_select ( "SELECT * FROM " . tablename (t_team) .
        " WHERE  company = '{$company}' order by groupid" );
    if (!empty($teamlist) && empty($team)){
        $result=array();
        $result['error']=1;
        $result['msg']='请选择所属团队';
        $result['data']='';
        return $result;
    }
    if (empty($_P['username'])){
        $result=array();
        $result['error']=1;
        $result['msg']='请输入昵称';
        $result['data']='';
        return $result;
    }
    if (empty($_P['mobile'])){
        $result=array();
        $result['error']=1;
        $result['msg']='请输入电话';
        $result['data']='';
        return $result;
    }
    //待开发
    // if ($system['tixing']==1 && $theme['status']==1) {
    //     $data = array(
    //         'server'  => DOMAIN0,
    //         'softname'  => MODULE_NAME,
    //         'uniacid'   => $_G['mpid'],
    //         'themeid'   => $themeid,
    //         'softkey'   => $theme['softkey'],
    //         'server_openid'     => $_P['server_openid'],
    //         'soft_openid'   => $fans['openid'],
    //         'username'=> $_P['username'],
    //         'mobile'  => $_P['mobile'],
    //         'zhiwu'  => $leveln,        
    //     );
    //     $qr = Poster::ServerQR($system,'save',$data);
    //     if ($qr['error']!=0) {
    //         return_msg($qr['msg'],$url,'error'); 
    //     }else{
    //         $qruserid = $qr['id'];
    //     }
    // }else{
        $qruserid = 0;
    // }
    $qrimg = Poster::base64_image_content($_P['qrcode']);
    $data = array (
        'uniacid' => $_G['mpid'],
        'nickname'=> $fans['nickname'],
        'avatar'  => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
        'uid'     => $_G['member']['uid'],
        'openid'  => $_G['openid'],
        // 'company' => $company,
        'team' => $team,
        'username'=> $_P['username'],
        'mobile'  => $_P['mobile'],
        'wechat'      => $_P['wechat'],
        'qrcode'      => $qrimg,
        'status'  => 1,
        'qruserid'  => $qruserid,
        'type'    => 'theme',
    );

    $alltheme = Theme::getAllTheme();
    if (!empty($alltheme)) {
        foreach ($alltheme as $key => $item) {
            $data ['themeid'] = $item['id'];
            $workersave = Worker::getThemeWorkerByOpenidzhuce($_G['openid'],$item['id'],1);
            if (empty($workersave)){
                $data ['addtime'] = TIMESTAMP;
                db_insert ( t_worker, $data );
            }else{
                db_update (t_worker, $data, array ('openid' => $_G['openid'],'themeid' => $item['id']));
            }
            //判断是否有设置首图海报，有则直接生成
            $themeid = $item['id'];
            $poster =  Poster::getByThemeIdnew($themeid,1);
            if(!empty($poster['id'])){
                $member = array(
                    "openid"   =>$fans['openid'],
                    'nickname' =>$fans['nickname'],
                    'avatar'   =>$fans['avatar'],
                    // 'shareurl' => $shareurl,
                    'themeid' =>$themeid,
                    'addtime' => TIMESTAMP,
                );
                $poster['bg']=str_replace("/99.jpg","/haibao.jpg",$poster['bg']);

                $img = Poster::createGoodsPoster($poster, $member, $qrimg);
                $hbimg = str_replace("http://", "https://", $img['img']) ;
                db_update(t_worker, array('poster' =>$hbimg), array( 'themeid' => $themeid, 'openid' => $_G['openid']));
            }
        }
    }
    db_query("UPDATE ".tablename(t_relation)." SET parentid = ".$_G['member']['uid'].",popenid='".$_G['openid']."',workerid = ".$_G['member']['uid']." WHERE openid ='".$_G['openid']."'");

    db_update(t_fans, array('poster' =>''), array('uid'=>$_G['member']['uid']));



}



}
?>